package cn.lsq.org.controller;

import cn.lsq.basic.utils.AjaxResult;
import cn.lsq.basic.utils.PageList;
import cn.lsq.org.domain.Department;
import cn.lsq.org.query.DepartmentQuery;
import cn.lsq.org.service.DepartmentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    @GetMapping("/{id}")
    public Department getOne(@PathVariable("id")Long id){
        return departmentService.loadById(id);
    }

    @GetMapping
    public List<Department> getAll(){
        return departmentService.loadAll();
    }

    @PostMapping
    public PageList<Department> getPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    @PutMapping
    public AjaxResult saveAndUpdate(@RequestBody Department department){
        try {
            if (department.getId()==null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"fuck");
        }


    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.remove(id);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"fuck");
        }
    }

    @PatchMapping
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            departmentService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"fuck");
        }
    }

    /**
     * 查询部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树-无限极接口")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}
